import random
import socket

class Muport:
    def __init__(self):
        pass
    # 获取未使用的端口(随机)
    def get_port(self,port=None):
        if not port:
            port = random.randint(10000, 65535)
            print(port)
        if self.is_port_in_use(port):
            return self.get_port()
        else:
            return port
    # 检测端口是否被使用
    def is_port_in_use(self, port, host="127.0.0.1")-> bool:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            return s.connect_ex((host, port)) == 0